#include "base_card.h"

#ifndef SET_CARD
#define SET_CARD(id,type,orien,ttype0,ttype1,ttype2,ttype3,_top,_right,_bottom,_left,weight,_cloister,_x,_y) {\
.card_id=id,\
.card_type=type,\
.card_orien = orien,\
.terrain_type[0] = ttype0, \
.terrain_type[1] = ttype1, \
.terrain_type[2] = ttype2, \
.terrain_type[3] = ttype3, \
.top = _top, \
.right = _right, \
.bottom = _bottom, \
.left = _left, \
.castle_weighting = weight,\
.is_cloister = _cloister, \
.x = _x,\
.y= _y,\
},\

#endif

static struct card _Card[TOTAL_CARD]={
	SET_CARD(0,CARD_TYPE1,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(1,CARD_TYPE1,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(2,CARD_TYPE1,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(3,CARD_TYPE1,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(4,CARD_TYPE1,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(5,CARD_TYPE2,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(6,CARD_TYPE2,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(7,CARD_TYPE2,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(8,CARD_TYPE3,ORIEN_N,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(9,CARD_TYPE3,ORIEN_N,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(10,CARD_TYPE3,ORIEN_N,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(11,CARD_TYPE4,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,1,-1,-1)
	SET_CARD(12,CARD_TYPE4,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,1,-1,-1)
	SET_CARD(13,CARD_TYPE4,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,1,-1,-1)
	SET_CARD(14,CARD_TYPE4,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,1,-1,-1)
	SET_CARD(15,CARD_TYPE5,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,1,-1,-1)
	SET_CARD(16,CARD_TYPE5,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,1,-1,-1)
	SET_CARD(17,CARD_TYPE6,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(18,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(19,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(20,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(21,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(22,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(23,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(24,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(25,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(26,CARD_TYPE7,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(27,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(28,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(29,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(30,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(31,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(32,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(33,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(34,CARD_TYPE8,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(35,CARD_TYPE9,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(36,CARD_TYPE9,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(37,CARD_TYPE9,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(38,CARD_TYPE9,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(39,CARD_TYPE10,ORIEN_N,TERRAIN_CASTLE,TERRAIN_CASTLE,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(40,CARD_TYPE11,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(41,CARD_TYPE11,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(42,CARD_TYPE11,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(43,CARD_TYPE11,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(44,CARD_TYPE12,ORIEN_N,TERRAIN_CASTLE,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(45,CARD_TYPE12,ORIEN_N,TERRAIN_CASTLE,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(46,CARD_TYPE12,ORIEN_N,TERRAIN_CASTLE,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(47,CARD_TYPE13,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(48,CARD_TYPE13,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(49,CARD_TYPE14,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(50,CARD_TYPE14,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(51,CARD_TYPE14,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(52,CARD_TYPE14,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(53,CARD_TYPE14,ORIEN_N,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(54,CARD_TYPE15,ORIEN_N,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(55,CARD_TYPE15,ORIEN_N,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(56,CARD_TYPE15,ORIEN_N,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(57,CARD_TYPE15,ORIEN_N,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(58,CARD_TYPE16,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(59,CARD_TYPE16,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(60,CARD_TYPE16,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_FARM,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(61,CARD_TYPE17,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(62,CARD_TYPE17,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(63,CARD_TYPE17,ORIEN_N,TERRAIN_FARM,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(64,CARD_TYPE18,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_FARM,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(65,CARD_TYPE18,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_FARM,NULL,NULL,NULL,NULL,2,0,-1,-1)
	SET_CARD(66,CARD_TYPE18,ORIEN_N,TERRAIN_CASTLE,TERRAIN_FARM,TERRAIN_CASTLE,TERRAIN_FARM,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(67,CARD_TYPE19,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(68,CARD_TYPE19,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(69,CARD_TYPE19,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(70,CARD_TYPE19,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
	SET_CARD(71,CARD_TYPE19,ORIEN_N,TERRAIN_ROAD,TERRAIN_ROAD,TERRAIN_CASTLE,TERRAIN_CASTLE,NULL,NULL,NULL,NULL,1,0,-1,-1)
};
// times 
// ===================
// 0		nothing
// 1		right 90 degree
// 2		right 180 degree
// 3		right 270 degree
// -1		left 90 degree
// -2		left 180 degree
// -3		left 270 degree
void card_rotate(int idx, int times)
{
	struct card *this;
	this = get_card(idx);
	if(this)
		this->card_orien = (this->card_orien + times + TERRAIN_DIRECTION_COUNT) % TERRAIN_DIRECTION_COUNT;
}
int terrain_type(int idx, int direction)
{
	struct card *this;
	this = get_card(idx);
	if(this)
		return this->terrain_type[(direction+this->card_orien)%TERRAIN_DIRECTION_COUNT];
	// this is NULL
	return TERRAIN_UNKNOWN;
}
struct card *get_card(int idx)
{
	if(idx<0 || idx>TOTAL_CARD)
		return NULL;
	return &_Card[idx];
}
void set_card_pos(int idx, int x, int y)
{
	struct card *this;
	this = get_card(idx);
	if(this)
	{
		this->x = x;
		this->y = y;
	}
}
void set_top(int target_id, int source_id)
{
	struct card *target, *source;
	target = get_card(target_id);
	source = get_card(source_id);
	if(!target || !source)
		return;
	target->top = source;
}
void set_right(int target_id, int source_id)
{
	struct card *target, *source;
	target = get_card(target_id);
	source = get_card(source_id);
	if(!target || !source)
		return;
	target->right = source;
}
void set_bottom(int target_id, int source_id)
{
	struct card *target, *source;
	target = get_card(target_id);
	source = get_card(source_id);
	if(!target || !source)
		return;
	target->bottom = source;
}
void set_left(int target_id, int source_id)
{
	struct card *target, *source;
	target = get_card(target_id);
	source = get_card(source_id);
	if(!target || !source)
		return;
	target->left = source;
}
int is_top_ok(int target_id, int source_id)
{
	if(terrain_type(target_id, TOP) == terrain_type(source_id, BOTTOM))
		return 1;
	return 0;
}
int is_right_ok(int target_id, int source_id)
{
	if(terrain_type(target_id, RIGHT) == terrain_type(source_id, LEFT))
		return 1;
	return 0;
}
int is_bottom_ok(int target_id, int source_id)
{
	if(terrain_type(target_id, BOTTOM) == terrain_type(source_id, TOP))
		return 1;
	return 0;
}
int is_left_ok(int target_id, int source_id)
{
	if(terrain_type(target_id, LEFT) == terrain_type(source_id, RIGHT))
		return 1;
	return 0;
}
